package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.BaiAsk;
import com.ruoyi.system.service.IBaiAskService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.service.ISysUserService;

/**
 * 健康咨询Controller
 * 
 * @author saruri
 * @date 2024-08-06
 */
@RestController
@RequestMapping("/ask")
public class BaiAskController extends BaseController
{
    @Autowired
    private IBaiAskService baiAskService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询健康咨询列表
     */

    @GetMapping("/list")
    public TableDataInfo list(BaiAsk baiAsk)
    {
        startPage();
        List<BaiAsk> list = baiAskService.selectBaiAskList(baiAsk);
        return getDataTable(list);
    }

    /**
     * 导出健康咨询列表
     */
    @RequiresPermissions("system:ask:export")
    @Log(title = "健康咨询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaiAsk baiAsk)
    {
        List<BaiAsk> list = baiAskService.selectBaiAskList(baiAsk);
        ExcelUtil<BaiAsk> util = new ExcelUtil<BaiAsk>(BaiAsk.class);
        util.exportExcel(response, list, "健康咨询数据");
    }

    /**
     *  获取健康咨询详细信息
     */


    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        BaiAsk baiAsk =baiAskService.selectBaiAskById(id);
        SysUser sysUser = userService.selectUserById(baiAsk.getUserId());
        baiAsk.setNickName(sysUser.getNickName());
        baiAsk.setUserName(sysUser.getUserName());
        baiAsk.setAvatar(sysUser.getAvatar());
        //获取当前的用户userId= SecurityUtils.getUserId();
        // 获取当前的用户userId
        Long currentUserId = SecurityUtils.getUserId();

        // 判断 baiAsk.getUserId() 是否等于当前用户的 userId
        if (!baiAsk.getUserId().equals(currentUserId)) {
            baiAsk.setMyAsk(0);
        } else {
            baiAsk.setMyAsk(1);
        }
        return success(baiAsk);
    }

    /**

    /**
     * 获取健康咨询详细信息最新得到 首页先试用
     */
    @GetMapping(value = "/index")
    public AjaxResult getIndex()
    {
        return success(baiAskService.selectBaiAskIndex());
    }



    /**
     * 新增健康咨询
     */
    @RequiresPermissions("system:ask:add")
    @Log(title = "健康咨询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaiAsk baiAsk)
    {
        baiAsk.setUserId(SecurityUtils.getUserId());
        return toAjax(baiAskService.insertBaiAsk(baiAsk));
    }

    /**
     * 修改健康咨询
     */
    @RequiresPermissions("system:ask:edit")
    @Log(title = "健康咨询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaiAsk baiAsk)
    {
        return toAjax(baiAskService.updateBaiAsk(baiAsk));
    }

    /**
     * 删除健康咨询
     */
    @RequiresPermissions("system:ask:remove")
    @Log(title = "健康咨询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baiAskService.deleteBaiAskByIds(ids));
    }
}
